
clear
close all
clear persistent
clear global
clear mex
drawnow;


Screen('Preference', 'VisualDebuglevel', 3); % 4 is default but it makes a painful white screen
Screen('Preference', 'ConserveVRAM', 4096); % my computer seems to like this setting
c=define_colors(256);
EPI_TTL_Pulse=0;
VBLTimestamp=[]; % doesn't always work
StimulusOnsetTime=[]; % doesn't always work
FlipTimestamp=[]; % flip time, when the display is actually presented
Missed=[];
Beampos=[];
when=0; % how many screen refreshes to wait before flip
dontclear=0;% 0=copy buffer forward and clear buffer; 1=copy buffer forward and leave the buffer as is. 2=flip the two (fastest)
dontsync=0; % 0=wait and sync; 1=don't wait but sinc; 2=neither wait nor sync
multiflip=0;% for multi screens, bad idea
screenid=1; % for multi screens, always select the primary monitor
getExtendedData=0;
getRawCode=1;
ListenChar(2);% shuts off keyboard output to windows
cscale=255;
ch=[1];% what key was pressed. start off with 2 data points
i=0;
j=0;
secs=getsecs;

screenid=2;
w=1; % set this above 0 if you want to invoke the screen function to take over the monitor.

if w>0
	[w, screenRect] = Screen('OpenWindow', screenid);%[0 0 0]
	center_x=screenRect(3)/2;
	center_y=screenRect(4)/2;
	SCREEN('fillrect',w,[0 0 0]*cscale);
	screen('flip',w); % first call to flip may take extra time
	secs(2)=getsecs;
	screen('DrawText',w,'start',center_x,center_y,[ 0  1  0]*cscale);%-screen('textwidth',w,kk{min(find(keyCode))})/2
	screen('flip',w);
	secs(3)=getsecs;  % secs(3)-secs(2) should equal refresh rate.
end
%kk=load_win_keyboard;
keyIsDown=0;
keyCode=0;
fprintf('\nstart test\n')
fprintf('  out,   in,(char)\n')
fprintf('%5d,%5d,(%s)\n',i,j,kbname(ch(end)))
	[keyIsDown,secsa,keyCode_last] = KbCheck; % Warning: mxSetLogical is deprecated and will not be supported in the future. Please refer to the Release Notes for information on updating your code

go=1;
while go
	pause(0.0001)
	[keyIsDown,secsa,keyCode] = KbCheck; % Warning: mxSetLogical is deprecated and will not be supported in the future. Please refer to the Release Notes for information on updating your code
	if any(keyCode-keyCode_last)>0 & keywasup
		keywasup=0;
		ch(end+1)=min(find(keyCode));
		secs(end+1)=secsa;
		if w>0
			switch 1
				case keyCode(48+1) % button 1
					screen('fillrect',w,c.BLUE);
				case keyCode(48+2) % button 2dsfccc
					screen('fillrect',w,c.YELLOW);
				case keyCode(48+3) % button 3
					screen('fillrect',w,c.GREEN);
				case keyCode(48+4) % button 4
					screen('fillrect',w,c.RED);
				case keyCode(48+5) % EPI TTL Pulse
					screen('fillrect',w,c.WHITE);
					EPI_TTL_Pulse=EPI_TTL_Pulse+1;
				case keyCode(48+6) % button 5
					screen('fillrect',w,c.CYAN);
				case keyCode(48+7) % button 6
					screen('fillrect',w,c.ORANGE);
				case keyCode(48+8) % button 7
					screen('fillrect',w,c.BLACK);
				case keyCode(48+9) % button 8
					screen('fillrect',w,c.MAGENTA);
				case keyCode(27) % ESC on pcwin
					go=0;
				otherwise
					screen('fillrect',w,c.BLACK);
					halfoffset=0;%round(screen('textwidth',w,kk{min(find(keyCode))})/2)
					screen('DrawText',w,kbname(ch(end)),center_x-halfoffset,center_y,c.GREEN);%-screen('textwidth',w,kk{min(find(keyCode))})/2
					%[newX,newY]=Screen('DrawText', windowPtr, text [,x] [,y] [,color] [,backgroundColor] [,yPositionIsBaseline] [,swapTextDirection]);
			end
			[VBLTimestamp(end+1) StimulusOnsetTime(end+1) FlipTimestamp(end+1) Missed(end+1) Beampos(end+1)] = Screen('Flip', w , when , dontclear , dontsync, multiflip);
		end
		j=j+1;% # of loops where something was taken from buffer
		fprintf([  repmat('\b',1,15+length(kbname(ch(end-1)))) '%5d,%5d,(%s)\n'],i,j,kbname(ch(end)))
		keyCode_last(ch(end))=1;
	else % no kbcheck
		keyCode_last=keyCode;
	i=i+1;% # of loops where nothing was taken from buffer
		fprintf([  repmat('\b',1,15+length(kbname(ch(end)))) '%5d,%5d,(%s)\n'],i,j,kbname(ch(end)))
		if keyIsDown==0 | (keyIsDown & min(find(keyCode))~=ch(end))
			keywasup=1;
		end
	end% if keyCode-keyCode_last
end
if w>0  % if window
	screen('fillrect',w,[ .5 .5 .5]*cscale);
	[VBLTimestamp(end+1) StimulusOnsetTime(end+1) FlipTimestamp(end+1) Missed(end+1) Beampos(end+1)] = Screen('Flip', w, when, dontclear, dontsync, multiflip);
	SCREEN('closeall');
end
fprintf('\nend test\n')
ListenChar(0)
clear screen

if length(secs)>=4
	secs4=secs(:)-secs(4);
else
	disp('no response data')
end

%98898769876987698769871221898769876987645787698769786123232323343983721616
%16273891432222111111234231231112389876666667767899999888811122343216712345
%67778787654678762111``124ebdfdhfdgfd234112gfs12276894494949994499494949499
%49383872727722771661161161616167238494949499978697869786978697869786879766
%768768796879678697869786978697866
